package com.logistic.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.ServletActionContext;

import com.alipay.util.UtilDate;
import com.logistic.common.Arith;
import com.logistic.common.Constants;
import com.logistic.common.Excel;
import com.logistic.common.MyDateUtil;
import com.logistic.common.UUIDGenerator;
import com.logistic.common.UidGeneratorByRule;
import com.logistic.domain.ConsumeRecord;
import com.logistic.domain.Integral;
import com.logistic.domain.ParaConfig;
import com.logistic.domain.PickupAddressCN;
import com.logistic.domain.User;
import com.logistic.domain.Waybill;
import com.logistic.page.PageView;
import com.logistic.page.QueryResult;
import com.logistic.service.AddressService;
import com.logistic.service.IntegralService;
import com.logistic.service.ParaConfigService;
import com.logistic.service.UserService;
import com.logistic.service.WaybillService;
import com.opensymphony.xwork2.ActionSupport;

public class WaybillAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private AddressService addressService;
	private UserService userService;
	private WaybillService waybillService;
	private IntegralService integralService;
	private ParaConfigService paraConfigService;
	
	//到货预报列表
	private List<Waybill> forestList; 
	
	public List<Waybill> getForestList() {
		return forestList;
	}

	public void setForestList(List<Waybill> forestList) {
		this.forestList = forestList;
	}

	// 根据不同的状态显示所有的订单,管理员的action
	private List<Waybill> waybillListByStatus;

	public List<Waybill> getWaybillListByStatus() {
		return waybillListByStatus;
	}

	// 显示每一个waybillTO
	private Waybill wbTO;

	public Waybill getWbTO() {
		return wbTO;
	}

	// 页面上的收货地址列表
	private List<PickupAddressCN> addressList;

	public List<PickupAddressCN> getAddressList() {
		return addressList;
	}

	// 页面上ups列表
	private List<Waybill> enterWaybillList;

	public List<Waybill> getEnterWaybillList() {
		return enterWaybillList;
	}

	// 获取页面上的收货地址和ups列表
	public String queryAdressListAndEenterWaybillList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String Uid = (String) request.getSession().getAttribute("Uid");
		addressList = addressService.findAddressByUid(Uid);
		enterWaybillList = waybillService.findEnterWaybillNum(Uid);
		//这个标志位是在menu.jsp确定是哪种转运
		String type = request.getParameter("type");
		//the flag of actionFlag is from struts.xml
		String flag = request.getParameter("actionFlag");
		if("submitSuccess".equals(flag)){
			request.setAttribute("customerSubmitFlag", "SUCCESS");
		}
		//这个是普通的提交运单
		if("common".equals(type))
		{
			return "getAddressListSuccess";
		}
		//这个是极速原箱提交运单
		else if("fast".equals(type))
		{
			Integral in = new Integral();
			in.setUid(Uid);
			in = integralService.getIntegralByUid(in);
			request.setAttribute("integralTO", in);
			return "getAddressListSuccessForFastSubmitWaybill";
		}
		return "getAddressListSuccess";
	}
	
	public void getWaybillByWareHouse(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String Uid = (String) request.getSession().getAttribute("Uid");
		addressList = addressService.findAddressByUid(Uid);
		enterWaybillList = waybillService.findEnterWaybillNum(Uid);
	}
	//查出来的运单
	private List<Waybill> currentWaybillList;

	public List<Waybill> getCurrentWaybillList() {
		return currentWaybillList;
	}

	// 当前运单的List
	private List<Waybill> searchList;

	public List<Waybill> getsearchList() {
		return searchList;
	}

	// 历史运单List
	private List<Waybill> historyWaybillList;

	public List<Waybill> getHistoryWaybillList() {
		return historyWaybillList;
	}

	// 分页获取waybillList表现形式
	private PageView pageView;

	public PageView getPageView() {
		return pageView;
	}

	public void setPageView(PageView pageView) {
		this.pageView = pageView;
	}

	// 分页显示当前运单
	public String queryCurrentWaybillByUid() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = (String) request.getAttribute("currentPage");
		String Uid = (String) request.getSession().getAttribute("Uid");
		QueryResult<Waybill> queryResult = null;
		if (null == currentPage) {
			// 参数1 每页显示的数目 参数2当前页
			pageView = new PageView<Waybill>(10, 1);
		} else {
			pageView = new PageView<Waybill>(10, Integer.parseInt(currentPage));// 默认是10条记录,第一页;
		}
		queryResult = waybillService.findCurrentWaybillByUid(Uid, pageView
				.getFirstResult(), pageView.getMaxresult());
		// queryResult=chengguoService.findAllCgProject(pageView.getFirstResult(),pageView.getMaxresult());
		pageView.setQueryResult(queryResult);// 触发生成页码等等
		request.setAttribute("pageView", pageView);
		currentWaybillList = queryResult.getResultlist();
		return "currentWaybillList";
	}

	// 分页显示历史运单
	public String queryHistoryWaybillByUid() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = (String) request.getAttribute("currentPage");
		String Uid = (String) request.getSession().getAttribute("Uid");
		QueryResult<Waybill> queryResult = null;
		if (null == currentPage) {
			// 参数1 ：每页显示的数目 参数2当前页
			pageView = new PageView<Waybill>(5, 1);
		} else {
			pageView = new PageView<Waybill>(5, Integer.parseInt(currentPage));// 默认是10条记录,第一页;
		}
		queryResult = waybillService.findHistoryWaybillByUid(Uid, pageView
				.getFirstResult(), pageView.getMaxresult());
		// queryResult=chengguoService.findAllCgProject(pageView.getFirstResult(),pageView.getMaxresult());
		pageView.setQueryResult(queryResult);// 触发生成页码等等
		request.setAttribute("pageView", pageView);
		historyWaybillList = queryResult.getResultlist();
		return "historyWaybillList";
	}
	
	public String searchWaybillForecast()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbUps = request.getParameter("wbUps");
		wbUps = wbUps.trim();
		String Uid = request.getParameter("Uid");
		Uid = Uid.trim();
		String wbStatus = request.getParameter("wbStatus");
		Waybill wb = new Waybill();
		wb.setWbUps(wbUps);
		wb.setUid(Uid);
		wb.setWbStatus(wbStatus);
		forestList = waybillService.searchWaybillForecast(wb);
		
		return "searchWaybillForecastSuccess";
	}
	
	public String searchWaybillUnhandle()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbUps = this.getWbUps().trim();
		String Uid = this.getUid().trim();
		String wbStatus = request.getParameter("wbStatus");
		String wbId = this.getWbId();
		String UreceiveId = this.getUreceiveId();
		String wbPUname = this.getWbPUname();
		Waybill wb = new Waybill();
		wb.setWbId(wbId);
		wb.setUreceiveId(UreceiveId);
		wb.setWbPUname(wbPUname);
		wb.setWbUps(wbUps);
		wb.setUid(Uid);
		wb.setWbStatus(wbStatus);
		forestList = waybillService.searchWaybillForecast(wb);
		
		return "searchWaybillUnhandleSuccess";
	}
	
	
	public String searchWaybill(){
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			request.setCharacterEncoding("GBK");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String beginTime = request.getParameter("beginTime");
		String endTime = request.getParameter("endTime");
		String wbId = request.getParameter("wbId");
		String wbSendOrderNum = request.getParameter("wbSendOrderNum");
		String Uid = request.getParameter("Uid");
		String wbStatus = request.getParameter("wbStatus");
		String flag = request.getParameter("flag");
		String wbSendOrderNumUSA = request.getParameter("wbSendOrderNumUSA");
		String wbPUname = this.getWbPUname();
		String wbUps = this.getWbUps();
		String UreceiveId = this.getUreceiveId();
		
		
		Waybill wb = new Waybill();
		wb.setBeginTime(beginTime);
		wb.setEndTime(endTime);
		wb.setWbId(wbId);
		wb.setWbSendOrderNum(wbSendOrderNum);
		wb.setUid(Uid);
		wb.setWbStatus(wbStatus);
		wb.setWbSendOrderNumUSA(wbSendOrderNumUSA);
		wb.setWbPUname(wbPUname);
		wb.setWbUps(wbUps);
		wb.setUreceiveId(UreceiveId);
		
		
		
		searchList = waybillService.searchByInput(wb);
		request.setAttribute("searchList", searchList);
		if("finance".equals(flag)){
			Double money = 0.00;
			Double weight = 0.00;
			if(searchList!=null && searchList.size()!=0){
				for(int i=0;i<searchList.size();i++){
					String m = searchList.get(i).getWbTotalMoney();
					String w = searchList.get(i).getWbActualWeight();
					//money = money + Double.parseDouble(m);
					if(m!=null){
						money = Arith.add(money, Double.parseDouble(m));
					}
					if(w!=null){
						weight = Arith.add(weight, Math.ceil(Double.parseDouble(w)));
					}
				}
				request.setAttribute("totalMoney", money);
				request.setAttribute("totalWeight", weight);
			}
			return "financeSearchSuccess";
		}else if("searchIndexPage".equals(flag)){
			if(searchList!=null){
				wbTO = searchList.get(0);
			}
			return "searchIndexPageSuccess";
		}else
		return "searchSuccess";
	}
	private String Ids;
	public String getIds() {
		return Ids;
	}

	public void setIds(String ids) {
		Ids = ids;
	}
	public String batchApprovalForForecast(){
		String []ids = ServletActionContext.getRequest().getParameterValues("wbIds");
		for(int i=0;i<ids.length;i++){
			Waybill wb = new Waybill();
			wb.setWbId(ids[i]);
			wb.setWbStatus(Constants.WB_ENTER_STATUS);
			waybillService.modify(wb);
		}
		return "approvalForecastsuccess";
	}
	
	//把我们的订单信息下载下来给速达网站，而为了得到一个运单号
	public void downloadExceltoSUDA(){
		String status = Constants.WB_UNPAYFOR_STATUS;
		Waybill wbSearch = new Waybill();
		wbSearch.setWbStatus(status);
		List <Waybill> searchList = waybillService.searchByInput(wbSearch);
		  List<List<String>> totalList = new ArrayList<List<String>>();
		  for(int i=0;i<searchList.size();i++){
				Waybill wb = searchList.get(i);
			    List<String> list = new ArrayList<String>();
			   // (付款人帐号)	Order	备注	支付方式	结款方式	付费类型	包装方式	寄件人签名	寄件日期	寄件地址	寄件方式	包装类型	派送方式	寄件人姓名	寄件人电话	寄件人公司名称	寄件人详细地址	寄件人邮政编码	出口国	收件人姓名	收件人电话	收件人公司名称	收件人详细地址	收件人邮政编码	进口国	总件数	实际重量	体积重量	DIM	CM	海关总申报值	商品名1	R1	量1	总价1	商品名2	R2	量2	总价2	商品名3	R3	量3	总价3	商品名4	R4	量4	总价4
			    list.add("tim");
			    list.add("");
			    list.add("");
			    list.add("寄件人付款Bill Sender");
			    list.add("现结");
			    list.add("寄件人付费 Shipper");
			    list.add("包裹 Package");
			    list.add("tim");
			    //寄件的日期写当天的
			    list.add(MyDateUtil.getCurrentFormattedDate());
			    list.add("加州");
			    list.add("自送 Drop off");
			    list.add("自包装");
			    list.add("");
			    list.add("tim");
			    list.add(wb.getWbId());
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add(wb.getWbPUname());
			    list.add(wb.getWbPUphone());
			    list.add(wb.getWbPUname());
			    list.add(wb.getWbPUcity()+" "+wb.getWbPUaddress());
			    list.add(wb.getWbPUzip());
			    list.add("中国");
			    list.add("1");
			    list.add(wb.getWbActualWeight());
			    list.add(wb.getWbActualWeight());
			    list.add("1*1*1");
			    list.add("");
			    list.add(wb.getWbApplyValue());
			    list.add(wb.getWbGoodsDesc());
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			    list.add("");
			   
			    totalList.add(list);
		  }
		  Excel excel = new Excel();
		  String templatefilePath =  "D:\\downExcel\\template1.xls";
		try {
			excel.writeExcelBaseOnTemplateNew(totalList,templatefilePath,"Sheet1","D:\\downExcel\\result1.xls");
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//download
		HttpServletResponse response = ServletActionContext.getResponse();
		excel.excelDownload(response,"D:\\downExcel\\result1.xls");   
		
	}
	
	//把我们的订单信息下载下来给联合快递，而为了得到一个运单号
	public void downloadExceltoLianHe(){
		String status = Constants.WB_UNPAYFOR_STATUS;
		Waybill wbSearch = new Waybill();
		wbSearch.setWbStatus(status);
		List <Waybill> searchList = waybillService.searchByInput(wbSearch);
		  List<List<String>> totalList = new ArrayList<List<String>>();
		  for(int i=0;i<searchList.size();i++){
				Waybill wb = searchList.get(i);
			    List<String> list = new ArrayList<String>();
			   // (付款人帐号)	Order	备注	支付方式	结款方式	付费类型	包装方式	寄件人签名	寄件日期	寄件地址	寄件方式	包装类型	派送方式	寄件人姓名	寄件人电话	寄件人公司名称	寄件人详细地址	寄件人邮政编码	出口国	收件人姓名	收件人电话	收件人公司名称	收件人详细地址	收件人邮政编码	进口国	总件数	实际重量	体积重量	DIM	CM	海关总申报值	商品名1	R1	量1	总价1	商品名2	R2	量2	总价2	商品名3	R3	量3	总价3	商品名4	R4	量4	总价4
			    list.add("Lax10127");
			    list.add("sharon");
			    list.add("626-261-9990");
			    list.add("14335 valona dr.");
			    list.add(Double.toString(Math.ceil(Double.parseDouble(wb.getWbActualWeight()))));
			    list.add(wb.getWbGoodsDesc());
			    list.add("1");
			    list.add(wb.getWbTotalMoney());
			    list.add(wb.getWbPUname());
			    list.add(wb.getWbPUphone());
			    list.add(wb.getWbPUcity()+" "+wb.getWbPUaddress());
			    list.add(wb.getWbPUzip());
			    list.add("");
			    list.add("zhongguo");
			    list.add("LAX");
			    list.add("中国");
			    list.add("1");
			    list.add(wb.getWbPUcity());
			    list.add("");
			    list.add(wb.getWbId());
			    
			    totalList.add(list);
		  }
		  Excel excel = new Excel();
		  String templatefilePath =  "D:\\downExcel\\templateLianHe.xls";
		try {
			excel.writeExcelBaseOnTemplateNew(totalList,templatefilePath,"Sheet1","D:\\downExcel\\resultLianHe.xls");
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//download
		HttpServletResponse response = ServletActionContext.getResponse();
		excel.excelDownload(response,"D:\\downExcel\\resultLianHe.xls");   
		
	}
	
	public void downloadExcel(){
		  String []ids = ServletActionContext.getRequest().getParameterValues("Ids");
		  List<List<String>> totalList = new ArrayList<List<String>>();
		  for(int i=0;i<ids.length;i++){
				Waybill wb = new Waybill();
				wb.setWbId(ids[i]);
				wb = waybillService.getWaybillById(wb);
			    List<String> list = new ArrayList<String>();
			    // {"订货日期","收件人","收件人地址1","收件人地址2","收件人城市","收件人省份","收件人邮编","收件人email","收件人电话","邮件内容","原始订单号",};
			    list.add(Integer.toString(i+1));
			    list.add("BH"+wb.getWbId()+"BZ");
			    list.add(wb.getWbSendOrderNum());
			    list.add("sender name");
			    list.add("sender address1");
			    list.add("");
			    list.add("city");
			    list.add("CA");
			    list.add("Sender TEl");
			    list.add("Sender Zip");
			    list.add(wb.getWbPUname());
			    list.add(wb.getWbPUaddress());
			    list.add("");
			    list.add(wb.getWbPUcity());
			    list.add(wb.getWbPUcity());
			    list.add("中国");
			    list.add("收件人邮编");
			    list.add(wb.getWbPUphone());
			    list.add(wb.getWbGoodsDesc());
			    list.add("$"+wb.getWbApplyValue());
			   
			    totalList.add(list);
		  }
		  Excel excel = new Excel();
		  
		//生成的excel的位置
		//  String filePath =  ServletActionContext.getRequest().getContextPath()+"\\tmp\\test.xls";    
		  String templatefilePath =  "D:\\downExcel\\template.xls";
        //OutputStream os = new FileOutputStream(filePath);
		//String [] title= {"订货日期","收件人","收件人地址1","收件人地址2","收件人城市","收件人省份","收件人邮编","收件人email","收件人电话","邮件内容","原始订单号",};
		
		try {
			excel.writeExcelBaseOnTemplate(totalList,templatefilePath,"Summary","D:\\downExcel\\result.xls");
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//download
		HttpServletResponse response = ServletActionContext.getResponse();
		excel.excelDownload(response,"D:\\downExcel\\result.xls");   
		
	}
	
	public void checkWaybillExist()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbUps = request.getParameter("wbUps");
		Waybill wb = new Waybill();
		wb.setWbUps(wbUps);
		List wbList = waybillService.searchWaybill(wb);
		HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType( "text/html" ); 
	    response.setCharacterEncoding( "UTF-8" );
	    
	    try {
			if(wbList == null ){
				response.getWriter().print("{success:true,message:'failed'}");
				return;
			}
			response.getWriter().print("{success:false,message:'ok'}");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//在待付款状态 管理员审批的时候看看账号金额是否足够
	public void checkMoney()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId  = (String)request.getAttribute("wbId");
		
		HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType( "text/html" ); 
	    response.setCharacterEncoding( "UTF-8" );
	     
	    try {
	    	Waybill old = new Waybill();
			old.setWbId(wbId);
			old = waybillService.getWaybillById(old);
			Double waybillTotalMoney = Double.parseDouble(old.getWbTotalMoney());
			Integral in = new Integral();
			in.setUid(old.getUid());
			in = integralService.getIntegralByUid(in);
			Double accoutMoney = Double.parseDouble(in.getaccoutMoney());
			if(accoutMoney < waybillTotalMoney){
				response.getWriter().print("{success:false,message:'failed'}");
				return;
			}
			response.getWriter().print("{success:true,message:'ok'}");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String searchFastBoxWaybill()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbUps = this.getWbUps().trim();
		Waybill wb = new Waybill();
		wb.setWbUps(wbUps);
		wb.setWbIsFastBox("Y");
		List wbList = waybillService.searchWaybill(wb);
		if(wbList != null)
		{
			//去极速原箱审批
			wbTO = (Waybill)wbList.get(0);
			request.setAttribute("wbTO", wbTO);
			return "fastToApprovalASuccess";
		}else{
			//去管理员录入界面
			return "enterWaybillSuccess";
		}
		
		
	}
	
	public String checkRemainMoney(String kouAnKey,String wbWarehouse){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session =  request.getSession(true);
		
		//分箱的要单独预估
		boolean isSplitBox = false;
		if ("1".equals(this.getWbOperateRequireType())) {
			if (!"1".equals(this.getWbTogetherSvc())
					&& "1".equals(this.getWbSplitSvc())) {
				//wb.setWbSplitNum(this.getWbSplitNum());
				isSplitBox = true;
			}
		}
		
		//得到配置参数
		ParaConfig pc = new ParaConfig();
		pc.setId(kouAnKey);
		pc.setUsaWarehouse(wbWarehouse);
		pc = paraConfigService.getParaConfigById(pc);
		Double unit_price = Double.parseDouble(pc.getUnit_price());
		Double exchange_rate = Double.parseDouble(pc.getExchange_rate());
		Double integral_ratio = Double.parseDouble(pc.getIntegral_ratio());
		Double first_price = Double.parseDouble(pc.getFirst_price());
		//是否用积分抵扣
		String usedIntegralFlag = pc.getUsedIntegralFlag();
		//多少磅以上可以积分抵扣
		Double startWeightUsedIntegral = Double.parseDouble(pc.getStartWeightUsedIntegral());
		//多少磅以上无首重
		Double startWeightNoFirst = Double.parseDouble(pc.getStartWeightNoFirst());
		
		
		//积分的表
		Integral inte = new Integral();
		inte.setUid((String)session.getAttribute("Uid"));
		inte = integralService.getIntegralByUid(inte);
		//账户余额
		Double accoutMoney = Double.parseDouble(inte.getaccoutMoney());
		//账户积分剩余
		Double strPoint = Double.parseDouble(inte.getPoint());

		//客户选择的订单号
		Double weight = 0.00;
		String choseEnterWayill[] = this.getWbChoseEnterWaybill().split(",");
		for(int i=0;i<choseEnterWayill.length;i++){
			Waybill oldWb = new Waybill();
			oldWb.setWbId(choseEnterWayill[i].trim());
			oldWb = waybillService.getWaybillById(oldWb);
			weight = Arith.add(weight, Double.parseDouble(oldWb.getWbEnterWeight())) ;
		}
		
		Double wbActualWeight = weight;
		Double wbEnterWeight = Math.ceil(weight);
		Double upWeight = Math.ceil(weight);
		//是否积分抵扣,这个是客户提交页面的是否积分抵扣
		boolean flagIsIntegral = false;
		//第一个是页面上是否选择了积分抵扣 第二个是后台配置的是否可以低分抵扣
		if ("Y".equals(this.getWbIsIntegral()) && "Y".equals(usedIntegralFlag)) {
			flagIsIntegral = true;
		}
		//是否保险
		boolean flagInsuranceFlag = false;
		if ("Y".equals(this.getWbInsuranceFlag())) {
			flagInsuranceFlag = true;
		}
		//申报价值
		Double applyValue = Double.parseDouble(this.getWbApplyValue());
		//仓库
		String houseWare = this.getWbWarehouse();
		//是否到达能到积分抵扣的重量
		boolean flagBigThanStartWeightUsedIntegral = ( wbActualWeight >= startWeightUsedIntegral )?true:false;
		//是否达到免首重的重量
		if( weight > startWeightNoFirst )
		{
			first_price = unit_price;
		}
		
		//总共要花的钱
		Double wbTotalMoney = 0.00;
		//不带保险
        if(!flagInsuranceFlag){
        	//不用积分
        	if(!flagIsIntegral || !flagBigThanStartWeightUsedIntegral){    
        		if( !"OR".equals(houseWare)){      
        			//总价格   
        			wbTotalMoney = Arith.mul(Arith.add(first_price, Arith.mul(Arith.sub(upWeight, 1), unit_price)), exchange_rate);
        			//如果是分箱加上分享数
        			if(isSplitBox){
        				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
        			}
  		    	}
  		    	if( "OR".equals(houseWare)){
  		    		wbTotalMoney = Arith.mul(Arith.add(wbEnterWeight, Arith.add(first_price, Arith.mul(Arith.sub(upWeight, 1), unit_price))), exchange_rate);
  		    		if(isSplitBox){
  		    			wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
        			}
  		    	}
        	}
        	//使用积分兑换
        	if(flagIsIntegral && flagBigThanStartWeightUsedIntegral){
        	//比较积分和总费用的10%大小关系
        		Double compare1 = Arith.mul(Arith.mul(Arith.add(first_price, Arith.mul(Arith.sub(upWeight, 1), unit_price)), exchange_rate), integral_ratio*0.01);
        		compare1 = Math.floor(compare1);
        		Double compare2 = strPoint;
        		if("OR".equals(houseWare)){
            		//积分够抵用10%的
            		if(compare2>=compare1){
            			wbTotalMoney = Arith.sub(Arith.mul(Arith.add(Arith.mul(wbEnterWeight, 1), Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1)))), exchange_rate), compare1);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}else//如果积分不够扣的
            		{
            			wbTotalMoney =  Arith.sub(Arith.mul(Arith.add(Arith.mul(wbEnterWeight, 1), Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1)))), exchange_rate), strPoint);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}
            	}
            	if(!"OR".equals(houseWare)){
            		//积分够抵用10%的
            		if(compare2>=compare1){
            			wbTotalMoney =  Arith.sub(Arith.mul(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), exchange_rate), compare1);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}else//如果积分不够扣的
            		{
            			wbTotalMoney = Arith.sub(Arith.mul(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), exchange_rate), strPoint);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}
            	}
        	}
        }
        
        //带上保险
        if(flagInsuranceFlag){
        	//不用积分
        	if(!flagIsIntegral || !flagBigThanStartWeightUsedIntegral){    
        		if( !"OR".equals(houseWare)){      
        			wbTotalMoney = Arith.mul(Arith.add(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), Arith.mul(applyValue, 0.03)), exchange_rate);
        			if(isSplitBox){
        				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
        			}
  		    	}
  		    	if("OR".equals(houseWare)){
  		    		wbTotalMoney = Arith.mul(Arith.add(Arith.mul(wbEnterWeight, 1), Arith.add(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), Arith.mul(applyValue, 0.03))), exchange_rate);
  		    		if(isSplitBox){
  		    			wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
        			}
  		    	}
  		    	
        	}
        	//使用积分兑换
        	if(flagIsIntegral && flagBigThanStartWeightUsedIntegral){    
        			//比较积分和总费用的10%大小关系
        		Double compare1 = Arith.mul(Arith.mul(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), exchange_rate), Arith.mul(integral_ratio,0.01));
        		compare1 = Math.floor(compare1);
        		Double compare2 = strPoint;
        		if( !"OR".equals(houseWare)){
            		//积分够抵用10%的
            		if(compare2>=compare1){  
            			wbTotalMoney = Arith.sub(Arith.mul(Arith.add(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), Arith.mul(applyValue, 0.03)), exchange_rate), compare1);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}else//如果积分不够扣的
            		{
            			wbTotalMoney = Arith.sub(Arith.mul(Arith.add(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), Arith.mul(applyValue, 0.03)), exchange_rate), strPoint);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}
            	}
            	if("OR".equals(houseWare)){
            		//积分够抵用10%的
            		if(compare2>=compare1){    
            			wbTotalMoney = Arith.sub(Arith.mul(Arith.add(Arith.mul(wbEnterWeight, 1), Arith.add(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), Arith.mul(applyValue, 0.03))), exchange_rate), compare1);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}else//如果积分不够扣的
            		{
            			wbTotalMoney = Arith.sub(Arith.mul(Arith.add(Arith.mul(wbEnterWeight, 1), Arith.add(Arith.add(first_price, Arith.mul(unit_price, Arith.sub(upWeight, 1))), Arith.mul(applyValue, 0.03))), exchange_rate), strPoint);
            			if(isSplitBox){
            				wbTotalMoney = Arith.add(wbTotalMoney,Arith.mul(Arith.mul(Double.parseDouble(this.getWbSplitNum()),unit_price),exchange_rate));
            			}
            		}
            	}
        	}
        	
        }
    
        if(accoutMoney>=wbTotalMoney) return "-1";
        else return Double.toString(wbTotalMoney);
	}
	
	

	// 客户提交新运单
	public String customerSubmitNewWaybill() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session =  request.getSession(true);
		
		String wbIsFastBox = this.getWbIsFastBox();
		
		if(!"Y".equals(wbIsFastBox))
		{
			String result = null;
			//result本次需要的钱
			if(this.getWbKouAn().equals("CQ")){
				result = checkRemainMoney("cqKey",this.getWbWarehouse());
			}else if(this.getWbKouAn().equals("GZ")){
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}else if(this.getWbKouAn().equals("GZBS")){
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}else if(this.getWbKouAn().equals("SHBS")){
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}else if(this.getWbKouAn().equals("SHBSDZ")){
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}else if(this.getWbKouAn().equals("FZ")){
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}else if(this.getWbKouAn().equals("BJ")){
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}else if(this.getWbKouAn().equals("TJ")){
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}else
			{
				result = checkRemainMoney(this.getWbKouAn(),this.getWbWarehouse());
			}
	        Double needMoney = Double.parseDouble(result);
			
			if(!"-1".equals(result)){
				request.setAttribute("accoutFlag", "noEnough");
				request.setAttribute("needMoney", needMoney);
				return "accoutNotEnough";
			}
		}
		
		//这个表示提交运单的时候选择的订单号, 如果是极速原箱的根本就没有，要重新创建一个
		String choseEnterWayill[] =  null ;
		if(!"Y".equals(wbIsFastBox))
		{
			choseEnterWayill = this.getWbChoseEnterWaybill().split(",");
		}
		if("Y".equals(wbIsFastBox))
		{
			Waybill wbTestDuplicate = new Waybill();
			wbTestDuplicate.setWbId(UidGeneratorByRule.getWaybillId());
			while(waybillService.getWaybillById(wbTestDuplicate)!=null){
				wbTestDuplicate.setWbId(UidGeneratorByRule.getWaybillId());
			}
			String tempArray[] = new String[1];
			tempArray[0] =  wbTestDuplicate.getWbId(); 
			choseEnterWayill = tempArray;
		}
		// 这个表示选择的ups是哪些
		
		// 这个表示选择的收获地址
		String choseAddress[] = this.getWbAddressList().split(",");

		// 如果选的是基本服务,那就对应的选择项做更新操作(基本服务里也有极速原箱过来的)
		if ("0".equals(this.getWbOperateRequireType())) {
			// 得到选择的运单号,在基本服务时只生成一个订单
			Waybill wb = new Waybill();
			//如果是极速原箱的,本来的预报的里的一些数据现在要添加上
			if("Y".equals(wbIsFastBox))
			{
				User u1 = new User();
				String tempUid = (String) session.getAttribute("Uid");
				u1.setUid(tempUid);
				u1 = userService.getUserByUid(u1);
				wb.setUreceiveId(u1.getUreceiveId());
				wb.setUid(tempUid);
				wb.setWbEnterWeight(this.getWbEnterWeight());
				wb.setWbUps(this.getWbUps());
				wb.setWbArea(this.getWbWarehouse());
				wb.setWbId(choseEnterWayill[0]);
				wb.setWbIsFastBox("Y");
			}
			//不是极速原箱的，普通的提交
			if(!"Y".equals(wbIsFastBox))
			{
				wb.setWbId(this.getWbChoseEnterWaybill().trim());
			}
			wb.setWbWarehouse(this.getWbWarehouse());
			wb.setWbGoodsDesc(this.getWbGoodsDesc());
			wb.setWbApplyValue(this.getWbApplyValue());
			wb.setWbKouAn(this.getWbKouAn());
			if (!"Y".equals(this.getWbInsuranceFlag())) {
				wb.setWbInsuranceFlag("N");
			} else {
				wb.setWbInsuranceFlag(this.getWbInsuranceFlag());
			}
			if (!"Y".equals(this.getWbIsIntegral())) {
				wb.setWbIsIntegral("N");
			} else {
				wb.setWbIsIntegral(this.getWbIsIntegral());
			}
			wb.setWbOperateRequireType(this.getWbOperateRequireType());
			wb.setWbPackqcfp(this.getWbPackqcfp());
			wb.setWbPackjjcl(this.getWbPackjjcl());
			wb.setWbPacknjjg(this.getWbPacknjjg());
			wb.setWbPackghwx(this.getWbPackghwx());
			wb.setWbPackjtwx(this.getWbPackjtwx());
			wb.setWbPackqcnj(this.getWbPackqcnj());
			wb.setWbPackyxzy(this.getWbPackyxzy());
			wb.setWbPackzdwpfx(this.getWbPackzdwpfx());
			wb.setWbNote(this.getWbNote());
			wb.setWbAddressList(this.getWbAddressList().trim());

			wb.setWbSubmitTime(getStringDate());
			wb.setWbStatus(Constants.WB_UNHANDLE_STATUS);

			// 在订单表输入选定的地址信息
			PickupAddressCN p = addressService.queryAddress(this
					.getWbAddressList().trim());
			wb.setWbPUcity(p.getPUarea());
			wb.setWbPUname(p.getPUname());
			wb.setWbPUzip(p.getPUzip());
			wb.setWbPUaddress(p.getPUaddress());
			wb.setWbPUphone(p.getPUphoneNum());
			// 找个原始订单，普通的提交订单
			if(!"Y".equals(wbIsFastBox))
			{
				Waybill oldWb = new Waybill();
				oldWb.setWbId(this.getWbChoseEnterWaybill().trim());
				oldWb = waybillService.getWaybillById(oldWb);
				wb.setWbAllUpsAndwWight(oldWb.getWbUps()+";"+oldWb.getWbEnterWeight()+";");
			}
			//极速原箱
			if("Y".equals(wbIsFastBox))
			{
				wb.setWbAllUpsAndwWight(wb.getWbUps()+";"+wb.getWbEnterWeight()+";");
			}
			//普通提交
			if(!"Y".equals(wbIsFastBox)){
				waybillService.modify(wb);
			}
			//极速原箱
			if("Y".equals(wbIsFastBox)){
				waybillService.save(wb);
			}
			
		} else {
			// 当客户选择了高级服务,单独点了合箱
			if ("1".equals(this.getWbTogetherSvc())
					&& !"1".equals(this.getWbSplitSvc())) {
				
				String allUpsAndWeight ="";
				for (int i = 0; i < choseEnterWayill.length; i++) {
					// 找个原始订单
					Waybill oldWb = new Waybill();
					oldWb.setWbId(choseEnterWayill[i].trim());
					oldWb = waybillService.getWaybillById(oldWb);
					allUpsAndWeight = allUpsAndWeight + oldWb.getWbUps()+";"+oldWb.getWbEnterWeight()+";";
				}
				
				for (int i = 0; i < choseEnterWayill.length; i++) {
					choseEnterWayill[i] = choseEnterWayill[i].trim();
					// 因为合箱时只生成一个订单，所以多余的要删除
					if (i == 0) {
						Waybill wb = new Waybill();
						wb.setWbId(choseEnterWayill[i]);
						wb.setWbWarehouse(this.getWbWarehouse());
						//仓库
						wb.setWbArea(this.getWbWarehouse());
						wb.setWbGoodsDesc(this.getWbGoodsDesc());
						wb.setWbApplyValue(this.getWbApplyValue());
						wb.setWbKouAn(this.getWbKouAn());
						if (!"Y".equals(this.getWbInsuranceFlag())) {
							wb.setWbInsuranceFlag("N");
						} else {
							wb.setWbInsuranceFlag(this.getWbInsuranceFlag());
						}
						if (!"Y".equals(this.getWbIsIntegral())) {
							wb.setWbIsIntegral("N");
						} else {
							wb.setWbIsIntegral(this.getWbIsIntegral());
						}
						wb.setWbOperateRequireType(this
								.getWbOperateRequireType());
						// 高级服务的3项 begin
						wb.setWbTogetherSvc(this.getWbTogetherSvc());
						wb.setWbSplitSvc(this.getWbSplitSvc());
						wb.setWbSplitNum(this.getWbSplitNum());
						// 高级服务的3向项end
						wb.setWbPackqcfp(this.getWbPackqcfp());
						wb.setWbPackjjcl(this.getWbPackjjcl());
						wb.setWbPacknjjg(this.getWbPacknjjg());
						wb.setWbPackghwx(this.getWbPackghwx());
						wb.setWbPackjtwx(this.getWbPackjtwx());
						wb.setWbPackqcnj(this.getWbPackqcnj());
						wb.setWbPackyxzy(this.getWbPackyxzy());
						wb.setWbPackzdwpfx(this.getWbPackzdwpfx());
						wb.setWbNote(this.getWbNote());
						// 合箱时只有一个收获地址
						wb.setWbAddressList(this.getWbAddressList());
						wb.setWbSubmitTime(getStringDate());
						wb.setWbStatus(Constants.WB_UNHANDLE_STATUS);

						// 在订单表输入选定的地址信息
						PickupAddressCN p = addressService
								.queryAddress(choseAddress[i]);
						wb.setWbPUcity(p.getPUarea());
						wb.setWbPUaddress(p.getPUaddress());
						wb.setWbPUname(p.getPUname());
						wb.setWbPUzip(p.getPUzip());
						wb.setWbPUphone(p.getPUphoneNum());
						
						wb.setWbAllUpsAndwWight(allUpsAndWeight);
						waybillService.modify(wb);
					} else {
						waybillService.deleteWaybillById(choseEnterWayill[i]);
					}
				}
			}

			// 当客户选择了高级服务,单独点了分箱
			if (!"1".equals(this.getWbTogetherSvc())
					&& "1".equals(this.getWbSplitSvc())) {
				//先生成分成的箱子的订单号(除去第一个箱子)
				String fxWbIds = this.getWbChoseEnterWaybill()+";";
				String [] fxWbIdArray = new String [choseAddress.length];
				fxWbIdArray[0] = this.getWbChoseEnterWaybill();
				int index = 1;
				for (int j = 1; j < 1000; j++) {
					Waybill existWb = new Waybill();
					String id = Integer.toString(Integer.parseInt(this
							.getWbChoseEnterWaybill())
							+ j);
					existWb.setWbId(id);
					if (waybillService.getWaybillById(existWb) != null) {
						continue;
					} else {
						fxWbIds = fxWbIds+id+";";
						fxWbIdArray[index] = id;
						index++;
						if(index == choseAddress.length) break;
					}
				}
				
				for (int i = 0; i < choseAddress.length; i++) {
					choseAddress[i] = choseAddress[i].trim();
					// 第一个是修改，以后是增加
					if (i == 0) {
						Waybill wb = new Waybill();
						wb.setWbId(this.getWbChoseEnterWaybill());
						wb.setWbWarehouse(this.getWbWarehouse());
						wb.setWbGoodsDesc(this.getWbGoodsDesc());
						wb.setWbApplyValue(this.getWbApplyValue());
						wb.setWbKouAn(this.getWbKouAn());
						if (!"Y".equals(this.getWbInsuranceFlag())) {
							wb.setWbInsuranceFlag("N");
						} else {
							wb.setWbInsuranceFlag(this.getWbInsuranceFlag());
						}
						if (!"Y".equals(this.getWbIsIntegral())) {
							wb.setWbIsIntegral("N");
						} else {
							wb.setWbIsIntegral(this.getWbIsIntegral());
						}
						wb.setWbOperateRequireType(this
								.getWbOperateRequireType());
						// 高级服务的3项 begin
						wb.setWbTogetherSvc(this.getWbTogetherSvc());
						wb.setWbSplitSvc(this.getWbSplitSvc());
						wb.setWbSplitNum(this.getWbSplitNum());
						// 高级服务的3向 end
						wb.setWbPackqcfp(this.getWbPackqcfp());
						wb.setWbPackjjcl(this.getWbPackjjcl());
						wb.setWbPacknjjg(this.getWbPacknjjg());
						wb.setWbPackghwx(this.getWbPackghwx());
						wb.setWbPackjtwx(this.getWbPackjtwx());
						wb.setWbPackqcnj(this.getWbPackqcnj());
						wb.setWbPackyxzy(this.getWbPackyxzy());
						wb.setWbPackzdwpfx(this.getWbPackzdwpfx());
						wb.setWbNote(this.getWbNote());
						// 合箱时只有一个收获地址
						wb.setWbAddressList(choseAddress[i]);
						wb.setWbSubmitTime(getStringDate());
						wb.setWbStatus(Constants.WB_UNHANDLE_STATUS);

						// 在订单表输入选定的地址信息
						PickupAddressCN p = addressService
								.queryAddress(choseAddress[i]);
						wb.setWbPUcity(p.getPUarea());
						wb.setWbPUaddress(p.getPUaddress());
						wb.setWbPUname(p.getPUname());
						wb.setWbPUzip(p.getPUzip());
						wb.setWbPUphone(p.getPUphoneNum());

						// 找个原始订单
						Waybill oldWb = new Waybill();
						oldWb.setWbId(this.getWbChoseEnterWaybill());
						oldWb = waybillService.getWaybillById(oldWb);
						wb.setWbArea(oldWb.getWbArea());
						wb.setWbAllUpsAndwWight(oldWb.getWbUps()+";"+oldWb.getWbEnterWeight()+";");
						wb.setWbSplitBoxRecord(fxWbIds);

						waybillService.modify(wb);
					} else {
						// 其他的是新增的,从第二个开始
						Waybill wb = new Waybill();
						wb.setWbId(fxWbIdArray[i]);
						// 新生成的订单,没有Uid号
						String Uid = (String) request.getSession().getAttribute("Uid");
						wb.setUid(Uid);
						//get UreceiveId
						User uu = new User();
						uu.setUid(Uid);
						uu=userService.getUserByUid(uu);
						wb.setUreceiveId(uu.getUreceiveId());
						
						wb.setWbWarehouse(this.getWbWarehouse());
						wb.setWbGoodsDesc(this.getWbGoodsDesc());
						wb.setWbApplyValue(this.getWbApplyValue());
						wb.setWbKouAn(this.getWbKouAn());
						if (!"Y".equals(this.getWbInsuranceFlag())) {
							wb.setWbInsuranceFlag("N");
						} else {
							wb.setWbInsuranceFlag(this.getWbInsuranceFlag());
						}
						if (!"Y".equals(this.getWbIsIntegral())) {
							wb.setWbIsIntegral("N");
						} else {
							wb.setWbIsIntegral(this.getWbIsIntegral());
						}
						wb.setWbOperateRequireType(this
								.getWbOperateRequireType());
						// 高级服务的3项 begin
						wb.setWbTogetherSvc(this.getWbTogetherSvc());
						wb.setWbSplitSvc(this.getWbSplitSvc());
						wb.setWbSplitNum(this.getWbSplitNum());
						// 高级服务的3向 end
						wb.setWbPackqcfp(this.getWbPackqcfp());
						wb.setWbPackjjcl(this.getWbPackjjcl());
						wb.setWbPacknjjg(this.getWbPacknjjg());
						wb.setWbPackghwx(this.getWbPackghwx());
						wb.setWbPackjtwx(this.getWbPackjtwx());
						wb.setWbPackqcnj(this.getWbPackqcnj());
						wb.setWbPackyxzy(this.getWbPackyxzy());
						wb.setWbPackzdwpfx(this.getWbPackzdwpfx());
						wb.setWbNote(this.getWbNote());
						// 合箱时只有一个收获地址
						wb.setWbAddressList(choseAddress[i]);
						wb.setWbSubmitTime(getStringDate());
						wb.setWbSendOrderNum(this.getWbSendOrderNum());
						wb.setWbStatus(Constants.WB_UNHANDLE_STATUS);
						// 在订单表输入选定的地址信息
						PickupAddressCN p = addressService
								.queryAddress(choseAddress[i]);
						wb.setWbPUcity(p.getPUarea());
						wb.setWbPUaddress(p.getPUaddress());
						wb.setWbPUname(p.getPUname());
						wb.setWbPUzip(p.getPUzip());
						wb.setWbPUphone(p.getPUphoneNum());
						// 找个原始订单
						Waybill oldWb = new Waybill();
						oldWb.setWbId(this.getWbChoseEnterWaybill());
						oldWb = waybillService.getWaybillById(oldWb);

						wb.setWbArea(oldWb.getWbArea());
						wb.setWbUps(oldWb.getWbUps());
						wb.setWbEnterWeight(oldWb.getWbEnterWeight());
						wb.setWbAllUpsAndwWight(oldWb.getWbUps()+";"+oldWb.getWbEnterWeight()+";");
						wb.setWbSplitBoxRecord(fxWbIds);

						waybillService.save(wb);
					}
				}

			}

			// 当客户选择了高级服务,是混合分箱
			if ("1".equals(this.getWbTogetherSvc())
					&& "1".equals(this.getWbSplitSvc())) {
				
				//这一部分是生成这一批混合分箱的新的订单号(先生成好放这)
				Waybill tmpWb = new Waybill();
				tmpWb.setWbId(UidGeneratorByRule.getWaybillId());
				while(waybillService.getWaybillById(tmpWb)!=null){
					tmpWb.setWbId(UidGeneratorByRule.getWaybillId());
				}
				String baseId = tmpWb.getWbId();
				//生成出来baseId的其他wbID
				//生成几个分箱订单的的数组和字符 分别给wbId(数组)和wbSplitBoxRecord(字符)用
				String fxWbIds = baseId+";";
				String [] fxWbIdArray = new String [choseAddress.length];
				fxWbIdArray[0] = baseId;
				int index = 1;
					for (int j = 1; j < 1000; j++) {
						Waybill existWb = new Waybill();
						String id = Integer.toString(Integer.parseInt(baseId)
								+ j);
						existWb.setWbId(id);
						if (waybillService.getWaybillById(existWb) != null) {
							continue;
						} else {
							fxWbIds = fxWbIds+id+";";
							fxWbIdArray[index] = id;
							index++;
							if(index == choseAddress.length) break;
						}
					}
				//生成wbAllUpsAndwWight
				String wbAllUpsAndwWight = "";
				for (int i = 0; i < choseEnterWayill.length; i++) {
					Waybill tp = new Waybill();
					tp.setWbId(choseEnterWayill[i].trim());
					tp = waybillService.getWaybillById(tp);
					wbAllUpsAndwWight = wbAllUpsAndwWight + tp.getWbUps()+";"+tp.getWbEnterWeight()+";";
				}
				for (int i = 0; i < choseAddress.length; i++) {
					
					choseAddress[i] = choseAddress[i].trim();
					// 对于混合分箱的策略 我是先增加 再删除
					Waybill wb = new Waybill();
					wb.setWbId(fxWbIdArray[i]);
					// 新生成的订单,没有Uid号
					String Uid = (String) request.getSession().getAttribute("Uid");
					wb.setUid(Uid);
					//get UreceiveId
					User uu = new User();
					uu.setUid(Uid);
					uu=userService.getUserByUid(uu);
					wb.setUreceiveId(uu.getUreceiveId());
					
					wb.setWbWarehouse(this.getWbWarehouse());
					//仓库的
					wb.setWbArea(this.getWbWarehouse());
					wb.setWbGoodsDesc(this.getWbGoodsDesc());
					wb.setWbApplyValue(this.getWbApplyValue());
					wb.setWbKouAn(this.getWbKouAn());
					if (!"Y".equals(this.getWbInsuranceFlag())) {
						wb.setWbInsuranceFlag("N");
					} else {
						wb.setWbInsuranceFlag(this.getWbInsuranceFlag());
					}
					if (!"Y".equals(this.getWbIsIntegral())) {
						wb.setWbIsIntegral("N");
					} else {
						wb.setWbIsIntegral(this.getWbIsIntegral());
					}
					wb.setWbOperateRequireType(this.getWbOperateRequireType());
					// 高级服务的3项 begin
					wb.setWbTogetherSvc(this.getWbTogetherSvc());
					wb.setWbSplitSvc(this.getWbSplitSvc());
					wb.setWbSplitNum(this.getWbSplitNum());
					// 高级服务的3向 end
					wb.setWbPackqcfp(this.getWbPackqcfp());
					wb.setWbPackjjcl(this.getWbPackjjcl());
					wb.setWbPacknjjg(this.getWbPacknjjg());
					wb.setWbPackghwx(this.getWbPackghwx());
					wb.setWbPackjtwx(this.getWbPackjtwx());
					wb.setWbPackqcnj(this.getWbPackqcnj());
					wb.setWbPackyxzy(this.getWbPackyxzy());
					wb.setWbPackzdwpfx(this.getWbPackzdwpfx());
					wb.setWbNote(this.getWbNote());
					wb.setWbAddressList(choseAddress[i]);
					wb.setWbSubmitTime(getStringDate());
					wb.setWbStatus(Constants.WB_UNHANDLE_STATUS);
					// 在订单表输入选定的地址信息
					PickupAddressCN p = addressService
							.queryAddress(choseAddress[i]);
					wb.setWbPUcity(p.getPUarea());
					wb.setWbPUaddress(p.getPUaddress());
					wb.setWbPUname(p.getPUname());
					wb.setWbPUzip(p.getPUzip());
					wb.setWbPUphone(p.getPUphoneNum());
					
					wb.setWbAllUpsAndwWight(wbAllUpsAndwWight);
					wb.setWbSplitBoxRecord(fxWbIds);
					

					waybillService.save(wb);

				}
				// 删除原来的
				for (int j = 0; j < choseEnterWayill.length; j++) {
					choseEnterWayill[j] = choseEnterWayill[j].trim();
					waybillService.deleteWaybillById(choseEnterWayill[j]);
				}
			}
		}
		
		
		//读取配置信息
		ParaConfig pc = new ParaConfig();
		pc.setId(this.getWbKouAn());
		pc.setUsaWarehouse(this.getWbWarehouse());
		pc = paraConfigService.getParaConfigById(pc);
		Integer presentIntegral = Integer.parseInt(pc.getPresentIntegral());
		Double presentMoney = Double.parseDouble(pc.getPresentMoney());
		String recommendFlag = pc.getRecommendFlag();
		if(recommendFlag.equals("Y")){
			//如果这个人是第一次提交运单,并且是被人推荐的,那么推荐的人可以拿到相应的奖励
			String Uid = (String) request.getSession().getAttribute("Uid");
			User submitUser = new User();
			submitUser.setUid(Uid);
			submitUser=userService.getUserByUid(submitUser);
			if( submitUser!=null && submitUser.getUrecommendBonusFlag()!=null && submitUser.getUrecommendBonusFlag().equals("N") && submitUser.getUrecommendId()!=null)
			{
				User recommendUser = new User();
				recommendUser.setUid(submitUser.getUrecommendId());
				recommendUser = userService.getUserByUid(recommendUser);
				if(recommendUser!=null){
					//增加积分和总钱数
					Integral inR = new Integral();
					inR.setUid(recommendUser.getUid());
					inR = integralService.getIntegralByUid(inR);
					//增加个人账号上的钱
					Double accoutMoney = Double.parseDouble(inR.getaccoutMoney());
					accoutMoney = Arith.add(accoutMoney, presentMoney);
					inR.setaccoutMoney(Double.toString(accoutMoney));
					//增加个人积分
					Integer currentPoint = Integer.parseInt(inR.getPoint());
					currentPoint = currentPoint+presentIntegral;
					inR.setPoint(Integer.toString(currentPoint));
					integralService.modify(inR);
					//增加一个消费记录,真有钱才会加入的
					if(presentMoney>0){
						//这边形成扣款记录
						ConsumeRecord record = new ConsumeRecord();
						record.setId(UUIDGenerator.getUUID());
						record.setuId(submitUser.getUrecommendId());
						//这个字段是给支付宝充值时的交易号
						record.setTradeNum("");
						record.setWbId("");
						record.setOperationTime(UtilDate.getDateFormatter());
						record.setAccountAction("shoukuan");
						record.setAmount(pc.getPresentMoney());
						record.setOperateType("tuijianyonghu");
						//这个是给支付宝判断重复充值的作用
						record.setSuccessFlag("");
						userService.createChargeRecord(record);		
					}
					
				}
				//奖励过以后，标志位置为Y,表示已经奖励过了
				submitUser.setUrecommendBonusFlag("Y");
				userService.modify(submitUser);
			}
		}
		
		//如果是极速原箱提交要返回极速原箱的页面
		if("Y".equals(wbIsFastBox))
		{
			return "fastWaybillSubmitSuccess";
		}

		return "waybillSubmitSuccess";
	}

	// 管理员录入运单信息
	public String enterWaybill() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String managerUid = request.getParameter("managerUid");
		
		//校验ups是否存在
		Waybill existWb = new Waybill();
		existWb.setUid(Uid);
		//属于某个人的所有订单
		boolean upsDuplite = false;
		List<Waybill> li = (List<Waybill>)waybillService.getWayillByUid(existWb);
		if(li!=null && li.size() !=0 ){
			for(int i=0;i<li.size();i++){
				if(this.getWbUps().equals(li.get(i).getWbUps())){
					upsDuplite = true;
					break;
				}
			}
		}
		if (upsDuplite) {
			request.setAttribute("enterWbFlag", "dup");
			return "enterWaybillSuccess";
		}
		
		// 如果这个客户不存在
		if (!userService.checkUidExist(this.getUid()) && "".equals(this.getUreceiveId())) {
			request.setAttribute("enterWbFlag", "N");
			return "enterWaybillSuccess";
		}
		
		if(!userService.checkUreceveIdExist(this.getUreceiveId()) && "".equals(this.getUid())){
			request.setAttribute("enterWbFlag", "N2");
			return "enterWaybillSuccess";
		}
		
		Waybill wb = new Waybill();
		if("".equals(this.getUreceiveId()) && !"".equals(this.getUid())){
			User u1 = new User();
			u1.setUid(this.getUid());
			u1 = userService.getUserByUid(u1);
			wb.setUreceiveId(u1.getUreceiveId());
			wb.setUid(this.getUid());
		}
		
		if(!"".equals(this.getUreceiveId()) && "".equals(this.getUid())){
			User u2 = new User();
			u2.setUreceiveId(this.getUreceiveId());
			u2 = userService.getUserByUreceiveId(u2);
			wb.setUid(u2.getUid());
			wb.setUreceiveId(this.getUreceiveId());
		}
		//取得管理员的Uarea，比如是CA还是HUSTON的
		//User user = new User();
		//user.setUid(managerUid);
		//user = userService.getUserByUid(user);
		//String wbArea = user.getUarea();
		
		
			wb.setWbId(UidGeneratorByRule.getWaybillId());
			while(waybillService.getWaybillById(wb)!=null){
				wb.setWbId(UidGeneratorByRule.getWaybillId());
			}
			
			wb.setWbEnterWeight(this.getWbEnterWeight());
			wb.setWbUps(this.getWbUps());
			wb.setWbStatus(Constants.WB_ENTER_STATUS);
			wb.setWbArea(this.getWbArea());
			wb.setWbEnterStatusTime(getStringDate());
			waybillService.save(wb);
			request.setAttribute("enterWbFlag", "Y");
		return "enterWaybillSuccess";
	}
	
	// 客户到货预报
	public String forecastWaybill() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(true);
		boolean upsDuplite = false;
		String Uid = (String) session.getAttribute("Uid");
		// session失效了
		if (null==Uid || "".equals(Uid)) {
			request.setAttribute("enterWbFlag", "N");
			return "forecastWaybillSuccess";
		}
		//校验ups是否存在
		Waybill existWb = new Waybill();
		existWb.setUid(Uid);
		//属于某个人的所有订单
		List<Waybill> li = (List<Waybill>)waybillService.getWayillByUid(existWb);
		if(li!=null && li.size() !=0 ){
			for(int i=0;i<li.size();i++){
				if(this.getWbUps().trim().equals(li.get(i).getWbUps())){
					upsDuplite = true;
					break;
				}
			}
		}
		if (upsDuplite) {
			request.setAttribute("enterWbFlag", "dup");
			return "forecastWaybillSuccess";
		}
		
		//根据Uid拿到ureceiveId
		User u1 = new User();
		u1.setUid(Uid);
		u1 = userService.getUserByUid(u1);
		String UreceiveId = u1.getUreceiveId();

		
		Waybill wb = new Waybill();
		
		wb.setWbId(UidGeneratorByRule.getWaybillId());
		while(waybillService.getWaybillById(wb)!=null){
			wb.setWbId(UidGeneratorByRule.getWaybillId());
		}
		wb.setUid(Uid);
		wb.setUreceiveId(UreceiveId);
		wb.setWbEnterWeight(this.getWbEnterWeight());
		wb.setWbUps(this.getWbUps());
		wb.setWbStatus(Constants.WB_FORECAST_STATUS);
		wb.setWbArea(this.getWbArea());
		wb.setWbForecastStatusTime(getStringDate());
		waybillService.save(wb);
		//到货预报成功
		request.setAttribute("enterWbFlag", "Y");
		return "forecastWaybillSuccess";
	}

	// 
	public String showForestWaybillByUid() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(true);
		String Uid = (String) session.getAttribute("Uid");
		Waybill wb = new Waybill();
		wb.setUid(Uid);
		wb.setWbStatus(Constants.WB_FORECAST_STATUS);
		forestList = waybillService.findWaybillByUidAndStatus(wb);
		
		return "forestListSuccess";
	}
	
	public String showAllwbByStatusWithoutPage()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String status = request.getParameter("status");
		if (null == status || "".equals(status)) {
			status = (String) request.getAttribute("status");
		}
		HttpSession session = request.getSession(true);
		String Uid = (String) session.getAttribute("Uid");
		User user = new User();
		user.setUid(Uid);
		user = userService.getUserByUid(user);
		String wbArea = user.getUarea();
		
		Waybill wb = new Waybill();
		wb.setWbArea(wbArea);
		wb.setWbStatus(status);
		
		forestList = waybillService.findWaybillsByStatusWithoutPage(wb);
		
		return "forestListSuccessWithoutPage";
	}

	// 显示管理员审批各种状态的运单的列表
	public String showAllwbByStatus() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = (String) request.getAttribute("currentPage");
		String status = request.getParameter("status");
		if (null == status || "".equals(status)) {
			status = (String) request.getAttribute("status");
		}
		HttpSession session = request.getSession(true);
		String Uid = (String) session.getAttribute("Uid");
		User user = new User();
		user.setUid(Uid);
		user = userService.getUserByUid(user);
		String wbArea = user.getUarea();
		QueryResult<Waybill> queryResult = null;
		if (null == currentPage) {
			// 参数1 ：每页显示的数目 参数2当前页
			pageView = new PageView<Waybill>(10, 1);
		} else {
			pageView = new PageView<Waybill>(10, Integer.parseInt(currentPage));// 默认是10条记录,第一页;
		}
		queryResult = waybillService.findWaybillsByStatus(wbArea,status, pageView
				.getFirstResult(), pageView.getMaxresult());
		pageView.setQueryResult(queryResult);// 触发生成页码等等
		request.setAttribute("pageView", pageView);
		waybillListByStatus = queryResult.getResultlist();
		if ("unhandleStatus".equals(status)) {
			return "unhandleStatus";
		} else {
			if ("unPayforStatus".equals(status)) {
				return "unPayforStatus";
			}else if ("unPayforStatus1".equals(status)) {
				return "unPayforStatus1";
			}else if("forecastStatus".equals(status)){
				return "forecastStatus";
			} else
				return "payforUnSendStatus";
		}
	}

	
	// 根据wbId来更新订单的状态
	public String managerApprovalAWaybill() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId = (String) request.getAttribute("wbId");
		String flag = (String) request.getParameter("flag");
		Waybill wb = new Waybill();
		wb.setWbId(wbId);
		wb.setWbStatus(this.getWbStatus());
		wb.setWbActualWeight(this.getWbActualWeight());
		wb.setWbBalanceDesc(this.getWbBalanceDesc());
		wb.setWbSendOrderNumUSA(this.getWbSendOrderNumUSA());
		wb.setWbSendOrderNum(this.getWbSendOrderNum());
		wb.setWbTotalMoney(this.getWbTotalMoney());
		
		if ("A".equals(flag)) {
			//管理员审批,看账号里的钱够不够分两种的,如果钱够了，直接扣钱，如果不够就放到待付款中
			String usedPoints = request.getParameter("usedPoints");
			Waybill old = new Waybill();
			old.setWbId(wbId);
			old = waybillService.getWaybillById(old);
			Integral in = new Integral();
			in.setUid(old.getUid());
			in = integralService.getIntegralByUid(in);
			
			//下面是处理积分的地方
			Integer currentPoint = Integer.parseInt(in.getPoint());
			Integer usedPoint =  Integer.parseInt(in.getUsedPoints());
			currentPoint = currentPoint-Integer.parseInt(usedPoints);
			usedPoint = usedPoint+Integer.parseInt(usedPoints);
			in.setPoint(Integer.toString(currentPoint));
			in.setUsedPoints(Integer.toString(usedPoint));
			
			
			Double haveMoney = Double.parseDouble(in.getaccoutMoney());
			Double needMoney = Double.parseDouble(this.getWbTotalMoney()); 
			
			
			//账上钱不够的情况
			if(haveMoney < needMoney){
				wb.setWbUnPayforStatusTime1(getStringDate());
				//钱不够,会到待付款中
				wb.setWbStatus(Constants.WB_UNPAYFOR_STATUS1);
				
			}else //这个是钱够的情况
			{
				wb.setWbUnPayforStatusTime(getStringDate());
				
				//这个在钱够的情况下修改当前余额和已用的钱
				Double accoutMoney = Double.parseDouble(in.getaccoutMoney());
				Double usedMoney = Double.parseDouble(in.getUsedMoney());
				accoutMoney = Arith.sub(accoutMoney, Double.parseDouble(this.getWbTotalMoney()));
				usedMoney = Arith.add(usedMoney, Double.parseDouble(this.getWbTotalMoney()));
				in.setaccoutMoney(Double.toString(accoutMoney));
				in.setUsedMoney(Double.toString(usedMoney));
				
				//这边形成扣款记录
				ConsumeRecord record = new ConsumeRecord();
				record.setId(UUIDGenerator.getUUID());
				record.setuId(old.getUid());
				//这个字段是给支付宝充值时的交易号
				record.setTradeNum("");
				record.setWbId(wbId);
				record.setOperationTime(UtilDate.getDateFormatter());
				record.setAccountAction("koukuan");
				record.setAmount(this.getWbTotalMoney());
				record.setOperateType("xitongkoukuan");
				//这个是给支付宝判断重复充值的作用
				record.setSuccessFlag("");
				userService.createChargeRecord(record);		
				
			}
			
			waybillService.modify(wb);
			integralService.modify(in);
			
//			wb.setWbUnPayforStatusTime(getStringDate());
//			waybillService.modify(wb);
//			//modify integral
//				String usedPoints = request.getParameter("usedPoints");
//				Waybill old = new Waybill();
//				old.setWbId(wbId);
//				old = waybillService.getWaybillById(old);
//				Integral in = new Integral();
//				in.setUid(old.getUid());
//				in = integralService.getIntegralByUid(in);
//				Integer currentPoint = Integer.parseInt(in.getPoint());
//				Integer usedPoint =  Integer.parseInt(in.getUsedPoints());
//				currentPoint = currentPoint-Integer.parseInt(usedPoints);
//				usedPoint = usedPoint+Integer.parseInt(usedPoints);
//				in.setPoint(Integer.toString(currentPoint));
//				in.setUsedPoints(Integer.toString(usedPoint));
//				
//				Double accoutMoney = Double.parseDouble(in.getaccoutMoney());
//				Double usedMoney = Double.parseDouble(in.getUsedMoney());
//				accoutMoney = Arith.sub(accoutMoney, Double.parseDouble(this.getWbTotalMoney()));
//				usedMoney = Arith.add(usedMoney, Double.parseDouble(this.getWbTotalMoney()));
//				in.setaccoutMoney(Double.toString(accoutMoney));
//				in.setUsedMoney(Double.toString(usedMoney));
//				
//				integralService.modify(in);
				
				return "approvalAsuccess";
		}else if("B1".equals(flag)){
			//这是从待付款页面过来的，还是要判断钱够不够，还有消费记录的数据
			Waybill old = new Waybill();
			old.setWbId(wbId);
			old = waybillService.getWaybillById(old);
			Integral in = new Integral();
			in.setUid(old.getUid());
			in = integralService.getIntegralByUid(in);
			
			//这个在钱够的情况下修改当前余额和已用的钱
			Double accoutMoney = Double.parseDouble(in.getaccoutMoney());
			Double usedMoney = Double.parseDouble(in.getUsedMoney());
			accoutMoney = Arith.sub(accoutMoney, Double.parseDouble(old.getWbTotalMoney()));
			usedMoney = Arith.add(usedMoney, Double.parseDouble(old.getWbTotalMoney()));
			in.setaccoutMoney(Double.toString(accoutMoney));
			in.setUsedMoney(Double.toString(usedMoney));
			integralService.modify(in);
			//这边形成扣款记录
			ConsumeRecord record = new ConsumeRecord();
			record.setId(UUIDGenerator.getUUID());
			record.setuId(old.getUid());
			//这个字段是给支付宝充值时的交易号
			record.setTradeNum("");
			record.setWbId(wbId);
			record.setOperationTime(UtilDate.getDateFormatter());
			record.setAccountAction("koukuan");
			record.setAmount(old.getWbTotalMoney());
			record.setOperateType("xitongkoukuan");
			//这个是给支付宝判断重复充值的作用
			record.setSuccessFlag("");
			userService.createChargeRecord(record);		
			//更改订单的状态
			wb.setWbUnPayforStatusTime(getStringDate());
			wb.setWbStatus(Constants.WB_UNPAYFOR_STATUS);
			waybillService.modify(wb);
			return "approvalB1success";
		}else if ("B".equals(flag)) {
			wb.setWbPayforUnSendStatusTime(getStringDate());
			waybillService.modify(wb);
			return "approvalBsuccess";
		} else if ("C".equals(flag)) {
			wb.setWbSendedStatusTime(getStringDate());
			waybillService.modify(wb);
			return "approvalCsuccess";
		} else if("forecast".equals(flag)){
			String modify = request.getParameter("modify");
			if(!"user".equals(modify)){
				//这是管理员修改
				wb.setWbEnterStatusTime(getStringDate());
				wb.setWbUps(this.getWbUps());
				wb.setWbEnterWeight(this.getWbEnterWeight());
				waybillService.modify(wb);
				String searchForecast = request.getParameter("flag2");
				if("searchForecast".equals(searchForecast)){
					return "approvalSearchForecastsuccess";
				}else
				return "approvalForecastsuccess";
			}else{
				//这是普通用户更新
				wb.setWbUps(this.getWbUps());
				wb.setWbEnterWeight(this.getWbEnterWeight());
				waybillService.modify(wb);
				return "deleteForestByUser";
			}
			
		}else
			return null;

	}
	public String verifyReceiveWaybill()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId = request.getParameter("wbId");
		if(wbId == null ) return "";
		Waybill wb = new Waybill();
		wb.setWbId(wbId);
		wb.setWbSendedStatusTime(getStringDate());
		wb.setWbStatus(Constants.WB_SENDED_STATUS);
		waybillService.modify(wb);
		return "verifyReceiveWaybillSuccess";
	}
	
	// 展示当前运单详情和历史运单详情
	// 查询统计里修改按钮
	public String showDetails() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String T = request.getParameter("T");
		String wbId = request.getParameter("wbId");
		if ("2".equals(T)) {
			// 历史运单详情
			// request.setAttrbute();控制服务态度评价的div显示
		}
		//查询页面修改之后的
		if ("query".equals(T)) {
			Waybill wb = new Waybill();
			wb.setWbId(wbId);
			wbTO = waybillService.getWaybillById(wb);
			return "showModifyWaybillDetailSuccess";
		}
		Waybill wb = new Waybill();
		wb.setWbId(wbId);
		wbTO = waybillService.getWaybillById(wb);
		return "showDetailSuccess";
	}
	
	
	public String updateWaybillByManager() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId = request.getParameter("wbId");
		Waybill wb = new Waybill();
		wb.setWbId(wbId);
		wbTO = waybillService.getWaybillById(wb);
		
		return "updateWaybillByManagerSuccess";
	}

	public String approvalWaybillA() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId = request.getParameter("wbId");
		String status = request.getParameter("status");
		Waybill wb = new Waybill();
		wb.setWbId(wbId);
		wbTO = waybillService.getWaybillById(wb);
		if ("unhandleStatus".equals(status)) {
			return "approvalWaybillA";
		}else if ("unPayforStatus1".equals(status)) {
			return "approvalWaybillB1";
		} else if ("unPayforStatus".equals(status)) {
			return "approvalWaybillB";
		} else if("forecastStatus".equals(status)){
			String flag = request.getParameter("flag");
			if("user".equals(flag)){
				//代表是普通用户
				return "waybillForecastByUser";
			}else if("searchForecast".equals(flag)){
				return "approvalWaybillForecastBySearch";
			}
			return "approvalWaybillForecast";
		}else
			return "approvalWaybillC";
	}

	public String deleteWaybillA() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId = request.getParameter("wbId");
		// get old
		Waybill oldWb = new Waybill();
		oldWb.setWbId(wbId);
		wbTO = waybillService.getWaybillById(oldWb);
		
		
		//如果是分箱操作,是要删除原来生成的其他订单
		if("1".equals(wbTO.getWbOperateRequireType()) && "1".equals(wbTO.getWbSplitSvc()) && null == wbTO.getWbTogetherSvc()){
			String [] names = wbTO.getWbSplitBoxRecord().split(";");
			for(int i=1;i<names.length;i++){
				waybillService.deleteWaybillById(names[i]);
			}
			Waybill wb = new Waybill();
			wb.setWbId(names[0]);
			wb.setUid(wbTO.getUid());
			wb.setUreceiveId(wbTO.getUreceiveId());
			wb.setWbEnterWeight(wbTO.getWbEnterWeight());
			wb.setWbArea(wbTO.getWbArea());
			wb.setWbUps(wbTO.getWbUps());
			wb.setWbStatus(Constants.WB_ENTER_STATUS);
			waybillService.deleteWaybillById(names[0]);
			waybillService.save(wb);
		}else // 合箱的情况删除
			if("1".equals(wbTO.getWbOperateRequireType()) && "1".equals(wbTO.getWbTogetherSvc()) && null == wbTO.getWbSplitSvc()){
				String [] allUpsAndWeight = wbTO.getWbAllUpsAndwWight().split(";");
				List<String> ups = new  ArrayList<String>();
				List<String> enterWeights = new  ArrayList<String>();
				for(int i=0;i<allUpsAndWeight.length;i++){
					if(i%2==0){
						ups.add(allUpsAndWeight[i]);
					}else{
						enterWeights.add(allUpsAndWeight[i]);
					}
				}
				//合箱时要生成一些新的
				for(int i=0;i<ups.size();i++){
					Waybill newWb = new Waybill();
					newWb.setWbId(UidGeneratorByRule.getWaybillId());
					while(waybillService.getWaybillById(newWb)!=null){
						newWb.setWbId(UidGeneratorByRule.getWaybillId());
					}
					newWb.setUid(wbTO.getUid());
					newWb.setUreceiveId(wbTO.getUreceiveId());
					newWb.setWbEnterWeight(enterWeights.get(i));
					newWb.setWbUps(ups.get(i));
					newWb.setWbArea(wbTO.getWbArea());
					newWb.setWbStatus(Constants.WB_ENTER_STATUS);
					waybillService.save(newWb);
				}
				waybillService.deleteWaybillById(wbId);
		}else //混合分箱的情况
			if("1".equals(wbTO.getWbOperateRequireType()) && "1".equals(wbTO.getWbTogetherSvc()) && "1".equals(wbTO.getWbSplitSvc())){
				//下面是分离出ups和enterWeight
				String [] allUpsAndWeight = wbTO.getWbAllUpsAndwWight().split(";");
				List<String> ups = new  ArrayList<String>();
				List<String> enterWeights = new  ArrayList<String>();
				for(int i=0;i<allUpsAndWeight.length;i++){
					if(i%2==0){
						ups.add(allUpsAndWeight[i]);
					}else{
						enterWeights.add(allUpsAndWeight[i]);
					}
				}
				//创建新的运单
				for(int i=0;i<ups.size();i++){
					Waybill newWb = new Waybill();
					newWb.setWbId(UidGeneratorByRule.getWaybillId());
					while(waybillService.getWaybillById(newWb)!=null){
						newWb.setWbId(UidGeneratorByRule.getWaybillId());
					}
					newWb.setUid(wbTO.getUid());
					newWb.setUreceiveId(wbTO.getUreceiveId());
					newWb.setWbEnterWeight(enterWeights.get(i));
					newWb.setWbUps(ups.get(i));
					newWb.setWbArea(wbTO.getWbArea());
					newWb.setWbStatus(Constants.WB_ENTER_STATUS);
					waybillService.save(newWb);
				}
				//下面是物理删除原来的
				String [] names = wbTO.getWbSplitBoxRecord().split(";");
				for(int i=0;i<names.length;i++){
					waybillService.deleteWaybillById(names[i]);
				}
				
			}else
		{ //这是原箱转发的情况
			// set new waybill
			Waybill wb = new Waybill();
			wb.setWbId(wbId);
			wb.setUid(wbTO.getUid());
			wb.setUreceiveId(wbTO.getUreceiveId());
			wb.setWbEnterWeight(wbTO.getWbEnterWeight());
			wb.setWbUps(wbTO.getWbUps());
			wb.setWbArea(wbTO.getWbArea());
			wb.setWbStatus(Constants.WB_ENTER_STATUS);
			waybillService.deleteWaybillById(wbId);
			waybillService.save(wb);
		}
		
		//删除以后，个人的预估钱数要减少啊

		return "deleteSubmitWb";
	}

	/**
	 * 删除
	 */
	public String delete() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String PUaddrId = (String) request.getAttribute("PUaddrId");
		waybillService.deleteWaybillById(PUaddrId);
		
		return "success";
	}
	
	//在查询页面的修改
	public String modifyWbInQuery()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		Waybill wb = new Waybill();
		wb.setWbId(this.getWbId());
		wb.setWbWarehouse(this.getWbWarehouse());
		wb.setWbSendOrderNum(this.getWbSendOrderNum());
		wb.setWbSendOrderNumUSA(this.getWbSendOrderNumUSA());
		wb.setWbStatus(this.getWbStatus());
		wb.setWbEnterWeight(this.getWbEnterWeight());
		wb.setWbUps(this.getWbUps());
		wb.setWbAllUpsAndwWight(this.getWbAllUpsAndwWight());
		wb.setWbActualWeight(this.getWbActualWeight());
		wb.setWbBalanceDesc(this.getWbBalanceDesc());
		wb.setWbTotalMoney(this.getWbTotalMoney());
		wb.setWbKouAn(this.getWbKouAn());
		wb.setWbGoodsDesc(this.getWbGoodsDesc());
		wb.setWbApplyValue(this.getWbApplyValue());
		wb.setWbInsuranceFlag(this.getWbInsuranceFlag());
		wb.setWbIsIntegral(this.getWbIsIntegral());
		wb.setWbPUname(this.getWbPUname());
		wb.setWbPUphone(this.getWbPUphone());
		wb.setWbPUzip(this.getWbPUzip());
		wb.setWbPUcity(this.getWbPUcity());
		wb.setWbPUaddress(this.getWbPUaddress());
		wb.setWbNote(this.getWbNote());
		
		request.setAttribute("modifyFlag", "success");
		waybillService.modify(wb);
		return "modifyFromQueryByAdmin";
	}
	//审批到货预报的时候删除
	public String deleteWaybillById() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId = (String) request.getAttribute("wbId");
		String userDeleteFlag = (String) request.getParameter("userFlag");
		waybillService.deleteWaybillById(wbId);
		if("user".equals(userDeleteFlag)){
			return "deleteForestByUser";
		}
		if("queryByAdmin".equals(userDeleteFlag))
		{
			request.setAttribute("deleteFlag", "success");
			return "deleteFromQueryByAdmin";
		}
		return "approvalForecastsuccess";
	}

	// 用来显示到货数量的
	public void resetWaybillNum() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(true);
		String Uid = (String) session.getAttribute("Uid");
		List enterWaybillList = waybillService.findEnterWaybillNum(Uid);
		try {
			if (enterWaybillList == null) {
				response.getWriter().print("{success:false}");
			} else {
				response.getWriter().print(
						"{success:true,num : " + enterWaybillList.size() + "}");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String printWaybill()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String wbId = (String) request.getParameter("wbId");
		Waybill wb = new Waybill();
		wb.setWbId(wbId);
		wbTO = waybillService.getWaybillById(wb);
		return "printSuccess";
	}

	// 获取固定格式的时间
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	// 各种service的注入
	public IntegralService getIntegralService() {
		return integralService;
	}

	public void setIntegralService(IntegralService integralService) {
		this.integralService = integralService;
	}
	public WaybillService getWaybillService() {
		return waybillService;
	}

	public void setWaybillService(WaybillService waybillService) {
		this.waybillService = waybillService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private String wbId;
	private String Uid;

	private String wbWarehouse;
	private String wbGoodsDesc;
	private String wbApplyValue;
	private String wbInsuranceFlag;
	private String wbOperateRequireType;
	private String wbTogetherSvc;
	private String wbSplitSvc;
	private String wbSplitNum;
	private String wbPackqcfp;
	private String wbPackjjcl;
	private String wbPacknjjg;
	private String wbPackghwx;
	private String wbPackjtwx;
	private String wbPackqcnj;
	private String wbPackyxzy;
	private String wbPackzdwpfx;
	private String wbNote;
	private String wbAddressList;
	private String wbSubmitTime;

	private String wbSendOrderNumUSA;
	private String wbSendOrderNum;
	private String wbStatus;
	private String wbUps;
	private String wbPUcity;
	private String wbPUaddress;
	private String wbPUname;
	private String wbPUphone;
	private String wbPUzip;
	private String wbBalanceDesc;
	private String wbEnterWeight;
	private String wbActualWeight;
	private String wbEnterStatusTime; // 管理员录入时间
	private String wbUnPayforStatusTime; // 客户已提交订单,管理员更改为待付款的时间
	private String wbPayforUnSendStatusTime;// (已付过钱，管理员更改为待发货的状态)
	private String wbSendedStatusTime;// (管理员更改为已发货时间)
	private String wbAcceptedStatusTime;// (客户收到货物时间)
	private String wbTotalMoney;// (单笔订单的总金额)
	private String wbArea;

	// 提交运单时客户选择要运输的运单
	private String wbChoseEnterWaybill;
	// 当前的页
	private String currentPage;

	public String getWbId() {
		return wbId;
	}

	public void setWbId(String wbId) {
		this.wbId = wbId;
	}

	public String getUid() {
		return Uid;
	}

	public void setUid(String uid) {
		Uid = uid;
	}

	public String getWbWarehouse() {
		return wbWarehouse;
	}

	public void setWbWarehouse(String wbWarehouse) {
		this.wbWarehouse = wbWarehouse;
	}

	public String getWbGoodsDesc() {
		return wbGoodsDesc;
	}

	public void setWbGoodsDesc(String wbGoodsDesc) {
		this.wbGoodsDesc = wbGoodsDesc;
	}

	public String getWbApplyValue() {
		return wbApplyValue;
	}

	public void setWbApplyValue(String wbApplyValue) {
		this.wbApplyValue = wbApplyValue;
	}

	public String getWbInsuranceFlag() {
		return wbInsuranceFlag;
	}

	public void setWbInsuranceFlag(String wbInsuranceFlag) {
		this.wbInsuranceFlag = wbInsuranceFlag;
	}

	public String getWbOperateRequireType() {
		return wbOperateRequireType;
	}

	public void setWbOperateRequireType(String wbOperateRequireType) {
		this.wbOperateRequireType = wbOperateRequireType;
	}

	public String getWbTogetherSvc() {
		return wbTogetherSvc;
	}

	public void setWbTogetherSvc(String wbTogetherSvc) {
		this.wbTogetherSvc = wbTogetherSvc;
	}

	public String getWbSplitSvc() {
		return wbSplitSvc;
	}

	public void setWbSplitSvc(String wbSplitSvc) {
		this.wbSplitSvc = wbSplitSvc;
	}

	public String getWbSplitNum() {
		return wbSplitNum;
	}

	public void setWbSplitNum(String wbSplitNum) {
		this.wbSplitNum = wbSplitNum;
	}

	public String getWbPackqcfp() {
		return wbPackqcfp;
	}

	public void setWbPackqcfp(String wbPackqcfp) {
		this.wbPackqcfp = wbPackqcfp;
	}

	public String getWbPackjjcl() {
		return wbPackjjcl;
	}

	public void setWbPackjjcl(String wbPackjjcl) {
		this.wbPackjjcl = wbPackjjcl;
	}

	public String getWbPacknjjg() {
		return wbPacknjjg;
	}

	public void setWbPacknjjg(String wbPacknjjg) {
		this.wbPacknjjg = wbPacknjjg;
	}

	public String getWbPackghwx() {
		return wbPackghwx;
	}

	public void setWbPackghwx(String wbPackghwx) {
		this.wbPackghwx = wbPackghwx;
	}

	public String getWbPackjtwx() {
		return wbPackjtwx;
	}

	public void setWbPackjtwx(String wbPackjtwx) {
		this.wbPackjtwx = wbPackjtwx;
	}

	public String getWbPackqcnj() {
		return wbPackqcnj;
	}

	public void setWbPackqcnj(String wbPackqcnj) {
		this.wbPackqcnj = wbPackqcnj;
	}

	public String getWbPackyxzy() {
		return wbPackyxzy;
	}

	public void setWbPackyxzy(String wbPackyxzy) {
		this.wbPackyxzy = wbPackyxzy;
	}

	public String getWbPackzdwpfx() {
		return wbPackzdwpfx;
	}

	public void setWbPackzdwpfx(String wbPackzdwpfx) {
		this.wbPackzdwpfx = wbPackzdwpfx;
	}

	public String getWbNote() {
		return wbNote;
	}

	public void setWbNote(String wbNote) {
		this.wbNote = wbNote;
	}

	public String getWbAddressList() {
		return wbAddressList;
	}

	public void setWbAddressList(String wbAddressList) {
		this.wbAddressList = wbAddressList;
	}

	public String getWbSubmitTime() {
		return wbSubmitTime;
	}

	public void setWbSubmitTime(String wbSubmitTime) {
		this.wbSubmitTime = wbSubmitTime;
	}

	public String getWbSendOrderNum() {
		return wbSendOrderNum;
	}

	public void setWbSendOrderNum(String wbSendOrderNum) {
		this.wbSendOrderNum = wbSendOrderNum;
	}

	public String getWbStatus() {
		return wbStatus;
	}

	public void setWbStatus(String wbStatus) {
		this.wbStatus = wbStatus;
	}

	public String getWbUps() {
		return wbUps;
	}

	public void setWbUps(String wbUps) {
		this.wbUps = wbUps;
	}

	public String getWbPUcity() {
		return wbPUcity;
	}

	public void setWbPUcity(String wbPUcity) {
		this.wbPUcity = wbPUcity;
	}

	public String getWbPUaddress() {
		return wbPUaddress;
	}

	public void setWbPUaddress(String wbPUaddress) {
		this.wbPUaddress = wbPUaddress;
	}
	public String getWbPUname() {
		return wbPUname;
	}

	public void setWbPUname(String wbPUname) {
		this.wbPUname = wbPUname;
	}

	public String getWbPUphone() {
		return wbPUphone;
	}

	public void setWbPUphone(String wbPUphone) {
		this.wbPUphone = wbPUphone;
	}
	public String getWbPUzip() {
		return wbPUzip;
	}

	public void setWbPUzip(String wbPUzip) {
		this.wbPUzip = wbPUzip;
	}

	public String getWbBalanceDesc() {
		return wbBalanceDesc;
	}

	public void setWbBalanceDesc(String wbBalanceDesc) {
		this.wbBalanceDesc = wbBalanceDesc;
	}

	public String getWbEnterWeight() {
		return wbEnterWeight;
	}

	public void setWbEnterWeight(String wbEnterWeight) {
		this.wbEnterWeight = wbEnterWeight;
	}

	public String getWbActualWeight() {
		return wbActualWeight;
	}

	public void setWbActualWeight(String wbActualWeight) {
		this.wbActualWeight = wbActualWeight;
	}

	public String getWbChoseEnterWaybill() {
		return wbChoseEnterWaybill;
	}

	public void setWbChoseEnterWaybill(String wbChoseEnterWaybill) {
		this.wbChoseEnterWaybill = wbChoseEnterWaybill;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getWbEnterStatusTime() {
		return wbEnterStatusTime;
	}

	public void setWbEnterStatusTime(String wbEnterStatusTime) {
		this.wbEnterStatusTime = wbEnterStatusTime;
	}
	public String getWbUnPayforStatusTime() {
		return wbUnPayforStatusTime;
	}

	public void setWbUnPayforStatusTime(String wbUnPayforStatusTime) {
		this.wbUnPayforStatusTime = wbUnPayforStatusTime;
	}
	public String getWbPayforUnSendStatusTime() {
		return wbPayforUnSendStatusTime;
	}

	public void setWbPayforUnSendStatusTime(String wbPayforUnSendStatusTime) {
		this.wbPayforUnSendStatusTime = wbPayforUnSendStatusTime;
	}

	public String getWbSendedStatusTime() {
		return wbSendedStatusTime;
	}

	public void setWbSendedStatusTime(String wbSendedStatusTime) {
		this.wbSendedStatusTime = wbSendedStatusTime;
	}

	public String getWbAcceptedStatusTime() {
		return wbAcceptedStatusTime;
	}

	public void setWbAcceptedStatusTime(String wbAcceptedStatusTime) {
		this.wbAcceptedStatusTime = wbAcceptedStatusTime;
	}

	public String getWbTotalMoney() {
		return wbTotalMoney;
	}

	public void setWbTotalMoney(String wbTotalMoney) {
		this.wbTotalMoney = wbTotalMoney;
	}
	
	private String UreceiveId;
	//记录哪些运单合成用的，删除可用
	private String wbAllUpsAndwWight;
	//给显示到具体分到哪几个箱子用
	private String wbSplitBoxRecord;
	//记录是否用积分
	private String wbIsIntegral;

	public String getWbIsIntegral() {
		return wbIsIntegral;
	}

	public void setWbIsIntegral(String wbIsIntegral) {
		this.wbIsIntegral = wbIsIntegral;
	}

	public String getUreceiveId() {
		return UreceiveId;
	}

	public void setUreceiveId(String ureceiveId) {
		UreceiveId = ureceiveId;
	}
	public String getWbAllUpsAndwWight() {
		return wbAllUpsAndwWight;
	}

	public void setWbAllUpsAndwWight(String wbAllUpsAndwWight) {
		this.wbAllUpsAndwWight = wbAllUpsAndwWight;
	}

	public String getWbSplitBoxRecord() {
		return wbSplitBoxRecord;
	}

	public void setWbSplitBoxRecord(String wbSplitBoxRecord) {
		this.wbSplitBoxRecord = wbSplitBoxRecord;
	}

	public String getWbArea() {
		return wbArea;
	}

	public void setWbArea(String wbArea) {
		this.wbArea = wbArea;
	}
	
	public String getWbSendOrderNumUSA() {
		return wbSendOrderNumUSA;
	}

	public void setWbSendOrderNumUSA(String wbSendOrderNumUSA) {
		this.wbSendOrderNumUSA = wbSendOrderNumUSA;
	}

	public ParaConfigService getParaConfigService() {
		return paraConfigService;
	}

	public void setParaConfigService(ParaConfigService paraConfigService) {
		this.paraConfigService = paraConfigService;
	}
	
	private String wbKouAn;

	public String getWbKouAn() {
		return wbKouAn;
	}

	public void setWbKouAn(String wbKouAn) {
		this.wbKouAn = wbKouAn;
	}
	//是否是极速原箱
	private String wbIsFastBox;

	public String getWbIsFastBox() {
		return wbIsFastBox;
	}

	public void setWbIsFastBox(String wbIsFastBox) {
		this.wbIsFastBox = wbIsFastBox;
	}

}
